{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cb82292a-21b5-45ab-acee-7e5c857bf7e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Father(object):\n",
    "    # 实例方法第一个参数必须是：self，代表实例本身\n",
    "    # 相当于 this 用法\n",
    "    def run(self):\n",
    "        print(\"run in father\")\n",
    "\n",
    "class Son(Father): # 继承\n",
    "    # 方法重写\n",
    "    def run(self):\n",
    "        super().run()  # 相当于 base()\n",
    "        print(\"run in son\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7bff7e06-d150-464a-add6-a39ac8fb747d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "run in father\n",
      "run in father\n",
      "run in son\n"
     ]
    }
   ],
   "source": [
    "obj = Father()\n",
    "obj.run()\n",
    "\n",
    "obj2 = Son()\n",
    "obj2.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "73a1abcc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(__main__.Son, __main__.Father, object)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看继承顺序\n",
    "Son.__mro__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e4db4c83",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 类装饰器 - 类方法\n",
    "# 类级别修改，方法可继承，常用来做工厂方法\n",
    "class Car:\n",
    "\n",
    "    _size = None # 类属性\n",
    "\n",
    "    # __new__构造函数（类方法），创建完会运行初始化函数（实例方法）__init__\n",
    "    # __del__销毁，释放对象\n",
    "    # 不重写为默认无参初始化函数__init__(self)\n",
    "    # 初始化不支持重载\n",
    "    def __init__(self, color, brand):\n",
    "        self.color = color  # 实例属性\n",
    "        self.brand = brand\n",
    "    @classmethod\n",
    "    def create(cls, color, brand): # 第一个参数必须是cls,代表类本身\n",
    "        instance = cls(color, brand)\n",
    "        return instance\n",
    "\n",
    "car = Car.create(\"red\",\"tesla\") # 类名直接调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "93cddca8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "static method\n"
     ]
    }
   ],
   "source": [
    "# 类装饰器 - 静态方法\n",
    "class Klass2:\n",
    "      @staticmethod\n",
    "      def func():\n",
    "          print(\"static method\")\n",
    "\n",
    "Klass2.func()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ebcb4e7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n"
     ]
    }
   ],
   "source": [
    "# 类装饰器 - 属性\n",
    "# 将方法装饰成属性\n",
    "class Klass3:\n",
    "    # get  读\n",
    "    @property\n",
    "    def name(self):\n",
    "        return self.__varName # 字段\n",
    "    # set  写\n",
    "    # @属性名.setter\n",
    "    @name.setter\n",
    "    def name(self, varValue):\n",
    "        self.__varName = varValue\n",
    "\n",
    "        \n",
    "        \n",
    "obj = Klass3()\n",
    "obj.name=\"123\"\n",
    "print(obj.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "062e30c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "dir(int)\n",
    "\n",
    "# 重写 > , 2.__gt__(11),将2实例化myint类型\n",
    "class myint(int):\n",
    "   def __gt__(self):\n",
    "      pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
